#encoding:utf8
import json , os , types , re

from framework.py_ex import *
from framework.inc import ReturnHelper
from functools import wraps
#=====================Decorators=======================#
#======================================================#

IS_LOGBODY = True
IS_LOGMSG  = True

#chk body is dict? || chk is api func?
def post_api_chk(method):   
    @wraps(method) 
    def wrapper(self, *args , **kwargs):
        postbody = self.request.body
        print (postbody, "--->.postbody")
        ret = None        
        global apiname
        apiname = ""
        rcv_dict = json.loads(postbody)
        
        if IS_LOGBODY:
            log_ex.dumpdic(rcv_dict)
        
        def _baseChk():
            #chk body is dict?
            if type(rcv_dict) != types.DictType:
                return ReturnHelper.error("client_body_decode")

            #is haved api func?
            global apiname 
            apiname = rcv_dict["tag"].encode("utf8")
            print (apiname ,"-->>apiname")
            try:
                self.__getattribute__(apiname)
            except:
                return ReturnHelper.error("handler_dont_exit")            
        ret = _baseChk()
        return method(self , ret , apiname , rcv_dict, *args , **kwargs)
    return wrapper

def onmsg_api_chk(method):
    @wraps(method)
    def wrapper(self , message , *args , **kwargs):
        ret = None
        rcv_dict = json.loads(message)
        if IS_LOGMSG:
            log_ex.dumpdic(rcv_dict)

        def _baseChk():
            #chk msg is dict 
            if type(rcv_dict) != types.DictType:
                return ReturnHelper.error("client_body_decode")

        ret = _baseChk()
        return method(self , message ,ret , rcv_dict , *args , **kwargs) 
    return wrapper